#pragma once

// #include "oj_model.hpp"
#include "oj_model2.hpp"
#include <ctemplate/template.h>

namespace ns_view
{
    using namespace ns_model;

    const std::string template_path = "./template_html/";   
    class View
    {
    public:
        View(){}
        ~View(){}
    public:
        void AllExpandHtml(const std::vector<Question> &all,std::string *html){
            //形成路径
            std::string path = template_path;
            path += "all_questions.html";

            //创建数据字典
            ctemplate::TemplateDictionary root("all_questions");
            for(auto &q : all)
            {
                ctemplate::TemplateDictionary *sub = root.AddSectionDictionary("question_list");
                sub->SetValue("number",q.number);
                sub->SetValue("title",q.title);
                sub->SetValue("star",q.star);
            }

            //获取要渲染的网页
            ctemplate::Template * tpl = ctemplate::Template::GetTemplate(path,ctemplate::DO_NOT_STRIP);
            
            //渲染网页
            tpl->Expand(html,&root);
        }
        void OneExpandHtml(Question &q,std::string *html){
            //形成路径
            std::string path = template_path;
            path += "one_question.html";

            //创建数据字典
            ctemplate::TemplateDictionary root("all_questions");
            root.SetValue("number",q.number);
            root.SetValue("title",q.title);
            root.SetValue("star",q.star);
            root.SetValue("desc",q.desc);
            root.SetValue("pre_code",q.header);

            //获取要渲染的网页
            ctemplate::Template * tpl = ctemplate::Template::GetTemplate(path,ctemplate::DO_NOT_STRIP);
            
            //渲染网页
            tpl->Expand(html,&root);
        }
    };
}